#!/usr/bin/python
# -*- coding: UTF-8 -*-

from lxml import etree
import sys
import hashlib
import requests
import json


def translate(data):
    """
    请求并返回结果
    """
    return requests.get(
        url='http://dict.youdao.com/w/eng/'+data

    )


def parse_e2z(html):
    # 音标
    pronounce = html.xpath(
        '/html/body/div[1]/div[2]/div[1]/div[2]/div[2]/div[1]/h2/div/span/span/text()')
    if len(pronounce) == 2:
        print('英', pronounce[0], ', 美', pronounce[1])

    # 意思
    nodes = html.xpath(
        "/html/body/div[1]/div[2]/div[1]/div[2]/div[2]/div[1]/div/ul/li/node()")
    for i in nodes:
        print(i)

    # 其他

    addition = html.xpath(
        '/html/body/div[1]/div[2]/div[1]/div[2]/div[2]/div[1]/div/p/text()')
    if len(addition) > 0:
        addition = str(addition[0])
        arr = addition[1:-2].split()
        i = 0
        j = len(arr)-1
        while (i < j):
            print(arr[i], arr[i+1])
            i += 2


def parse_z2e(html):
    nodes = html.xpath(
        '/html/body/div[1]/div[2]/div[1]/div[2]/div[2]/div[1]/div/ul/p/span/a/text()')
    for i in nodes:
        print(i)


# 打印提示信息
if len(sys.argv) < 2:
    print('Usage t <src>')
else:
    word = sys.argv[1]
    resp = translate(sys.argv[1])
    html = etree.HTML(resp.content)

    if word.isascii():
        # print("en")
        parse_e2z(html)
    else:
        # print("zh")
        parse_z2e(html)
